package com.bank.backend.boot.server.service.impl;


import com.bank.backend.boot.base.constant.ExceptionConstant;
import com.bank.backend.boot.base.model.dto.BankBaseDTO;
import com.bank.backend.boot.base.model.entity.BankBase;
import com.bank.backend.boot.base.model.exception.GlobalException;
import com.bank.backend.boot.server.mapper.BankBaseMapper;
import com.bank.backend.boot.server.converter.BankBaseConverter;
import com.bank.backend.boot.server.service.BankBaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author JiaoWei
* @description 针对表【bank_base(银行总行)】的数据库操作Service实现
* @createDate 2025-03-24 19:01:27
*/
@Service
public class BankBaseServiceImpl extends ServiceImpl<BankBaseMapper, BankBase>
    implements BankBaseService {

    @Resource
    private BankBaseMapper bankBaseMapper;

    @Resource
    private BankBaseConverter bankBaseConverter;

    /**
     * 根据总行名称查询总行信息
     * @param name
     * @return
     */
    @Override
    public BankBaseDTO queryBankBaseByName(String name) {
        BankBase bankBase = bankBaseMapper.queryBankBaseByName(name);
        return bankBaseConverter.convert(bankBase);
    }

    /**
     * 保存总行信息
     * @param name
     * @return
     */
    @Override
    public boolean saveBankBase(String name) {
        BankBase bankBase = bankBaseMapper.queryBankBaseByName(name);
        if (null != bankBase) {
           throw new GlobalException(ExceptionConstant.NAME_EXIST);
        }
        BankBaseDTO bankBaseDTO = new BankBaseDTO();
        bankBaseDTO.setName(name);
        BankBase convert = bankBaseConverter.convert(bankBaseDTO);
        convert.setCreateTime(new Date());
        convert.setUpdateTime(new Date());
        int i = bankBaseMapper.insert(convert);
        if (i == 0) {
            return false;
        }
        return true;
    }
}




